package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.base.BaseBomInfo;
import com.ruoyi.system.domain.base.TreeShop;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.mapper.BaseBomInfoMapper;
import com.ruoyi.system.service.IBaseBomInfoService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * bom详情Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-17
 */
@Service
public class BaseBomInfoServiceImpl extends ServiceImpl<BaseBomInfoMapper, BaseBomInfo> implements IBaseBomInfoService {

    @Override
    public String checkBomNameUnique(BaseBomInfo baseBomInfo) {
        Long bonId = StringUtils.isNull(baseBomInfo.getId()) ? -1L : baseBomInfo.getId();
        BaseBomInfo info = baseMapper.selectOne(new QueryWrapper<BaseBomInfo>().eq("material_name", baseBomInfo.getMaterialName())
                .eq("parent_id", baseBomInfo.getParentId()));
        if (StringUtils.isNotNull(info) && info.getId().longValue() != bonId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int insertShop(BaseBomInfo baseBomInfo) {
        BaseBomInfo info = baseMapper.selectOne(new QueryWrapper<BaseBomInfo>().eq("id", baseBomInfo.getId()));
        // 如果父节点不为正常状态,则不允许新增子节点
        if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
        {
            throw new CustomException("工厂停用，不允许新增");
        }
        baseBomInfo.setAncestors(info.getAncestors() + "," + baseBomInfo.getParentId());
        return baseMapper.insert(baseBomInfo);
    }

    @Override
    public List<BaseBomInfo> selectBomInfoList(BaseBomInfo baseBomInfo) {
        QueryWrapper<BaseBomInfo> qw = new QueryWrapper<BaseBomInfo>();
        if(null!=baseBomInfo.getBomId()){
            qw.eq("bom_id",baseBomInfo.getBomId());
        }
        List<BaseBomInfo> shops = baseMapper.selectList(qw);
        return shops;
    }

    @Override
    public List<TreeShop> buildBomInfoTreeSelect(List<BaseBomInfo> infos) {
        List<BaseBomInfo> shopTrees = buildBomInfoTree(infos);
        return shopTrees.stream().map(TreeShop::new).collect(Collectors.toList());
    }

    /**
     * 是否存在子节点
     *
     * @param id 工厂id
     * @return 结果
     */
    @Override
    public boolean hasChildByDeptId(Long id)
    {
        int result = baseMapper.hasChildByDeptId(id);
        return result > 0 ? true : false;
    }

    public List<BaseBomInfo> buildBomInfoTree(List<BaseBomInfo> infos) {
        List<BaseBomInfo> returnList = new ArrayList<BaseBomInfo>();
        List<Long> tempList = new ArrayList<Long>();
        for (BaseBomInfo dept : infos)
        {
            tempList.add(dept.getId());
        }
        for (Iterator<BaseBomInfo> iterator = infos.iterator(); iterator.hasNext();)
        {
            BaseBomInfo dept = (BaseBomInfo) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId()))
            {
                recursionFn(infos, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = infos;
        }
        return returnList;
    }

    private void recursionFn(List<BaseBomInfo> list, BaseBomInfo t) {
        // 得到子节点列表
        List<BaseBomInfo> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BaseBomInfo tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                // 判断是否有子节点
                Iterator<BaseBomInfo> it = childList.iterator();
                while (it.hasNext())
                {
                    BaseBomInfo n = (BaseBomInfo) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<BaseBomInfo> getChildList(List<BaseBomInfo> list, BaseBomInfo t)
    {
        List<BaseBomInfo> tlist = new ArrayList<BaseBomInfo>();
        Iterator<BaseBomInfo> it = list.iterator();
        while (it.hasNext())
        {
            BaseBomInfo n = (BaseBomInfo) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<BaseBomInfo> list, BaseBomInfo t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

}
